package com.mogist.hqc.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;

import com.mogist.hqc.R;
import com.mogist.hqc.base.MyApplication;
import com.mogist.hqc.entitys.event.WXEvent;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.greenrobot.eventbus.EventBus;

import talex.zsw.basecore.util.LogTool;
import talex.zsw.basecore.view.other.RxToast;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler
{
	@Override public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.entry);

		//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
		{
			//透明状态栏
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
		((MyApplication) getApplication()).getWxApi().handleIntent(getIntent(), this);
	}

	@Override protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);

		setIntent(intent);
	}

	@Override public void onReq(BaseReq req)
	{
	}

	@Override public void onResp(BaseResp resp)
	{
		//登录回调
		switch(resp.errCode)
		{
			case BaseResp.ErrCode.ERR_OK:
				String code = ((SendAuth.Resp) resp).code;
				EventBus.getDefault().post(new WXEvent(code));
				finish();
				break;
			case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
				LogTool.a("用户拒绝授权");
				RxToast.error("您已拒绝登录授权");
				finish();
				break;
			case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
				LogTool.a("用户取消");
				RxToast.error("您已取消登录授权");
				finish();
				break;
			default:
				finish();
				break;
		}
	}
}